package com.zjitc.serviceedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zjitc.serviceedu.entity.EduOrder;
import com.zjitc.serviceedu.mapper.EduOrderMapper;
import com.zjitc.serviceedu.service.EduOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author 赵诚挚
 * @since 2024-10-22
 */
@Service
public class EduOrderServiceImpl extends ServiceImpl<EduOrderMapper, EduOrder> implements EduOrderService {
    @Autowired
    private EduOrderMapper orderMapper;
    @Override
    public boolean saveOrder(EduOrder eduOrder) {
        String orderNo = UUID.randomUUID().toString().replace("-", "").substring(0, 18);
        eduOrder.setOrderNo(orderNo);
        int result = orderMapper.insert(eduOrder);
        return result > 0; // 如果插入成功，返回 true
    }

    @Override
    public boolean updateOrderStatus(String orderNo, int status) {
        // 根据订单号查找订单
        QueryWrapper<EduOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no", orderNo);

        // 获取订单
        EduOrder order = this.getOne(queryWrapper);
        if (order == null) {
            throw new RuntimeException("订单未找到");
        }

        // 更新订单状态
        order.setStatus(status);

        // 保存修改后的订单
        return this.updateById(order);  // 使用MyBatis-Plus的updateById方法
    }

    @Override
    public EduOrder getByOrderNo(String orderNo) {
        QueryWrapper<EduOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no", orderNo);  // 使用订单号进行查询
        return this.getOne(queryWrapper);  // 返回匹配的订单信息
    }


}
